//
//  MBGoodsDetailViewController.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/5/4.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBGoodsDetailViewController.h"
#import "MBImageGalleryViewController.h"
#import "MBGoodsDetailTypeView.h"
#import "MBScrollContentView.h"
#import "MBGoodsDetailImageContentView.h"
#import "MBGoodsDetailView.h"
#import "MBGoodsDetailCommentListView.h"

@interface MBGoodsDetailViewController ()<UIScrollViewDelegate,MBGoodsDetailCommentListViewDelegate>
@property (nonatomic,strong)MBScrollContentView* contentView;
@property (nonatomic,weak) UIScrollView* scrollView;
@property (nonatomic,weak) MBGoodsDetailView* goodsDetailView;

@property (nonatomic,weak)MBGoodsDetailTypeView* typeView;

@end

@implementation MBGoodsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupLayout];
    
    [self setUpRacValue];
}

-(void)setupLayout
{
    [super setupLayout];
    
    self.naviView.titleLabel.text = @"商品";
    self.naviView.titleLabel.hidden = YES;
    [self.naviView setUpBackButton];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleScrollComment:) name:NOTIFICATION_GOODS_DETAIL_SCROLL_COMMENT object:nil];
    
    MBGoodsDetailTypeView* typeView = [[MBGoodsDetailTypeView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-[UIView lf_sizeFromIphone6:210])/2,7+SCREEN_STATUS_BAR_HEIGHT, [UIView lf_sizeFromIphone6:210], 30)];
    self.typeView = typeView;
    [self.naviView addSubview:typeView];
    
    UIScrollView* scrollView = [[UIScrollView alloc]init];
    self.scrollView = scrollView;
    scrollView.scrollEnabled = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(44+SCREEN_STATUS_BAR_HEIGHT, 0, 0, 0));
    }];
    
    MBScrollContentView* scrollContentView = [[MBScrollContentView alloc]init];
    self.contentView = scrollContentView;
    CGFloat contentHeight = (SCREEN_HEIGHT-44-SCREEN_STATUS_BAR_HEIGHT);
    scrollContentView.contentHeight = contentHeight*2;
    [scrollView addSubview:scrollContentView];
    
    [scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(scrollView);
        make.top.equalTo(scrollView);
        make.height.mas_equalTo(contentHeight*2);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    UIScrollView* firstScrollView = [[UIScrollView alloc]init];
    firstScrollView.contentOffset = CGPointMake(0, 0);
    firstScrollView.userInteractionEnabled = YES;
    firstScrollView.directionalLockEnabled = YES;
    firstScrollView.delegate = self;
    firstScrollView.showsHorizontalScrollIndicator = NO;
    firstScrollView.showsVerticalScrollIndicator = NO;
    scrollContentView.firstScrollView = firstScrollView;

    MBGoodsDetailView* goodsDetailView = [[MBGoodsDetailView alloc]init];
    self.goodsDetailView = goodsDetailView;
    goodsDetailView.infoModel = self.infoModel;
    [firstScrollView addSubview:goodsDetailView];
    
    CGFloat goodsDetailheight = [goodsDetailView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize].height;
    if (goodsDetailheight < contentHeight) {
        goodsDetailheight = contentHeight+1;
    }
    
    [goodsDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(firstScrollView);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH,goodsDetailheight));
    }];
    
    firstScrollView.contentSize = CGSizeMake(SCREEN_WIDTH,goodsDetailheight);
    scrollContentView.fistScrollViewHeight = goodsDetailheight;
    
    UIScrollView* secondScrollView = [[UIScrollView alloc]init];
    secondScrollView.contentOffset = CGPointMake(0, 0);
    secondScrollView.userInteractionEnabled = YES;
    secondScrollView.directionalLockEnabled = YES;
    secondScrollView.delegate = self;
    secondScrollView.showsHorizontalScrollIndicator = NO;
    secondScrollView.showsVerticalScrollIndicator = NO;
    scrollContentView.secondScrollView = secondScrollView;
    
    MBGoodsDetailImageContentView* bottomContentView = [[MBGoodsDetailImageContentView alloc]init];
    bottomContentView.infoModel = self.infoModel;
    [secondScrollView addSubview:bottomContentView];
    
    [bottomContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.left.equalTo(secondScrollView);
    }];
    
    MBGoodsDetailCommentListView* commentListView = [[MBGoodsDetailCommentListView alloc]init];
    commentListView.infoModel = self.infoModel;
    commentListView.delegate = self;
    [scrollView addSubview:commentListView];
    
    [commentListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scrollView);
        make.left.equalTo(scrollView).with.offset(SCREEN_WIDTH);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, contentHeight));
    }];
}

-(void)setUpRacValue
{
    @weakify(self);
    [RACObserve(self.typeView, chooseType)subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        int type = [x intValue];
        if (type != 0) {
            if (type == 1) {
                self.scrollView.contentOffset = CGPointMake(0, 0);
                [self scrollToFirstScrollView];
            }else if (type == 2){
                self.scrollView.contentOffset = CGPointMake(0, 0);
                [self scrollToSecondScrollView];
            }else {
                self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
            }
        }
    }];
    
    [RACObserve(self.goodsDetailView.bannerView, clickImageUrl)subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        NSString* url = x;
        if (url) {
            MBImageGalleryViewController * chatGalleryController = [[MBImageGalleryViewController alloc] init];
            chatGalleryController.isBanner = YES;
            chatGalleryController.allMessageArray =  self.infoModel.gallery; chatGalleryController.selectIndex = [self.infoModel.gallery indexOfObject:url];
            [self.navigationController pushViewController:chatGalleryController animated:YES];
        }
    }];
    
    [RACObserve(self.goodsDetailView.commentView.imageContantView,imageIndex) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        int index = [x intValue];
        if (index > 100) {
            MBImageGalleryViewController * chatGalleryController = [[MBImageGalleryViewController alloc] init];
            chatGalleryController.isBanner = YES;
            MBCommentModel* model = self.infoModel.comment[0];
            chatGalleryController.allMessageArray = model.img;
            chatGalleryController.selectIndex = index;
            [self.navigationController pushViewController:chatGalleryController animated:YES];
        }
    }];
    
    [RACObserve(self.contentView, scrollNumView) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x intValue] != 0) {
            if ([x intValue] == 1) {
                [self scrollToFirstScrollView];
            }else {
                [self scrollToSecondScrollView];
            }
        }
    }];
    
    [[self.naviView.backButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];

}

-(void)handleScrollComment:(NSNotification*)notifi
{
    NSDictionary* dic = notifi.userInfo;
    BOOL isBanner = [[dic objectForKey:@"isBanner"] boolValue];
    if (isBanner) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }else {
        self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        self.typeView.type = 2;
    }
}

-(void)scrollToFirstScrollView {
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView);
        make.left.equalTo(self.scrollView);
        make.height.mas_equalTo(self.contentView.contentHeight);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
    }];
    self.typeView.type = 0;
}

-(void)scrollToSecondScrollView {
    
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView).with.offset(-(self.contentView.contentHeight/2));
        make.left.equalTo(self.view);
        make.height.mas_equalTo(self.contentView.contentHeight);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.view layoutIfNeeded];
    }];
    self.typeView.type = 1;
}

#pragma mark -MBGoodsDetailCommentListViewDelegate
-(void)goodsDetailCommentListViewClickImageWithCommentModel:(MBCommentModel *)commentModel andIndex:(int)index
{
    MBImageGalleryViewController * chatGalleryController = [[MBImageGalleryViewController alloc] init];
    chatGalleryController.isBanner = NO;
    chatGalleryController.allMessageArray = commentModel.img;
    chatGalleryController.selectIndex = index;
    [self.navigationController pushViewController:chatGalleryController animated:YES];
}



@end
